class Bug < ActiveRecord::Base
  belongs_to :developer, :class_name => "Member"
  belongs_to :reviewer, :class_name => "Member"
  belongs_to :release
  has_many :comments
  validates_presence_of :release, :on=>:update

  validate :cant_close_if_open_comments
  validate :review_comments_present

  def cant_close_if_open_comments

    if (status == 'Done' and status == 'Ready for build' and status == 'Closed as not an issue' or status == 'Considered already fixed') then
      comments_open = false

      comments.each do |comment|
        comments_open = true if comment.status != 'Verified' and comment.status != 'Closed'
      end

      errors.add :status, "Please close/verify all open comments" if  comments_open
    end
  end

  def review_comments_present

    if (status == 'Ready for build') then
      no_review_comments = true

      if comments.size > 0 then
        comments.each do |comment|
          no_review_comments = false if comment.status == 'Verified' or comment.status == 'Closed'
        end
      end

      errors.add :status, "Please have at least one comment that indicates code is reviewed" if  no_review_comments
    end
  end

  def defect?
    "Defect".eql?(defect_type)
  end

end
